
# pickles ingredient alc % information and drink_ing amount information
# this code was written in the context of the dm, so you will have to modify it to work elsewhere
# also, you need to import Fraction with is in Python 2.6+


# get a mapping from ingredientIDs to alc. %
ing_alc_raw = self.db.getAllIngredientIDsAndAlcoholContents()
ing_alc = {}
for i in ing_alc_raw:
    alc = 0
    if len(i[1].split("%"))>1:
        alc_str = i[1].split("%")[0]
        alc = float(alc_str)/100
    ing_alc[i[0]] = alc
pickle.dump(ing_alc,open("ingredients_alcohol.pickle","w"))

get ingredient lists for each drink, parse the number of ounces,
and save it for that drink's id
drink_ingr_amount = {} # {drinkID:{[(ingredientID,ounces),]},}
drink_ids = self.db.getAllDrinkIDs()
for d in drink_ids:
    drink_ingrs = {}
    #print "drink: "+str(d)
    i_info = self.db.getDrinkIngredientsPositionsAmounts(d)
    for i in i_info:
        #print "i_info: " + str(i_info)
        amt_str = i[2]
        if amt_str==None: amt_str = "" # keep loop going
        #print "amt_str: " + amt_str
        unit = None
        numunit = 0
        if len(amt_str.split("oz"))>1: unit = "oz"
        if len(amt_str.split("shot"))>1: unit = "shot"
        if len(amt_str.split("tsp"))>1: unit = "tsp"
        if len(amt_str.split("tblsp"))>1: unit = "tblsp"
        if len(amt_str.split("cl"))>1: unit = "cl"
        if len(amt_str.split("cup"))>1: unit = "cup"
        if unit:
            #print "unit: " + unit
            chunk = amt_str.split(unit)[0].strip()
            # add the fraction components of the string...
            comps = chunk.split(" ")
            #print "comps: " + str(comps)
            for c in comps:
                try: # random parsing errors occur... all sorts of strange things in there!
                    c.strip() # just in case
                    cc = c
                    # there could be a dash (e.g. 6-8 ounces) -- take the high end
                    if len(cc.split("-"))>1:
                        cc = cc.split("-")[1]
                    numunit += float(Fraction(cc))
                except:
                  print sys.exc_info()
                  continue
            #print "numunit: " + str(numunit)
        #convert the unit to ounces...
        #print "numunit: " + str(numunit)
        ounces = numunit
        if unit == "shot": ounces = numunit*1.5
        if unit == "tsp": ounces = numunit*.166666667
        if unit == "tblsp": ounces = numunit*.5
        if unit == "cl": ounces = numunit*.338140227
        if unit == "cup": ounces = numunit*8
        #print "ounces: " + str(ounces)
        drink_ingrs[i[1]] = (i[0],ounces)
    drink_ingr_amount[d] = drink_ingrs
# print drink_ingr_amount
# pickle.dump(drink_ingr_amount,open("drinks_ingredients_ounces.pickle","w"))
# 
# 
# # load ingredientID -> alc % mapping
# ingredients_alcohol = pickle.load(open("ingredients_alcohol.pickle","r"))
# # load drinkID -> ingredientPosition -> (ingredientID,ounces) mapping
# drink_ingr_ounces = pickle.load(open("drinks_ingredients_ounces.pickle","r"))
# 
# print ingredients_alcohol
# print drink_ingr_ounces